package com.haircut.sys.controller.dept;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haircut.sys.constant.Constants;
import com.haircut.sys.service.dept.IDeptService;
import com.haircut.sys.vo.dept.*;
import com.modular.result.PageResult;
import com.modular.result.RspMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/b/dept")
public class DeptController {
    @Autowired
    private IDeptService deptService;
    /**
     * 新增部门
     * @param reqVo
     * @return
     */
    @ApiOperation(value = "新增部门")
    @PostMapping("saveDept")
    public RspMessage<Void> saveDept(@Validated @RequestBody DeptReqVo reqVo){
        deptService.saveDept(reqVo);
        return RspMessage.success();
    }
    /**
     * 修改部门
     * @param reqVo
     * @return
     */
    @ApiOperation(value = "修改部门")
    @PostMapping("modifyDept")
    public RspMessage<Void> modifyDept(@Validated @RequestBody DeptReqVo reqVo){
        deptService.modifyDept(reqVo);
        return RspMessage.success();
    }
    /**
     * 删除角色
     * @param deptId
     * @return
     */
    @ApiOperation(value = "删除角色")
    @GetMapping("deleteById")
    public RspMessage<Void> deleteById(@ApiParam("部门id") @RequestParam("deptId") String deptId){
        deptService.deleteById(deptId);
        return RspMessage.success();
    }
    /**
     * 查询角色详情
     * @param deptId
     * @return
     */
    @ApiOperation(value = "查询角色详情")
    @GetMapping("findDetail")
    public RspMessage<DeptDetailVo> findDetail(@ApiParam("部门id") @RequestParam("deptId") String deptId){
        return RspMessage.success(deptService.findDetail(deptId));
    }
    /**
     * 查询角色列表
     */
    @ApiOperation(value = "查询角色列表")
    @PostMapping("findAll")
    public RspMessage<List<DeptPageResVo>> findAll(@RequestBody DeptPageReqVo reqVo){
        return RspMessage.success(deptService.findAll(reqVo));
    }

    /**
     * 查询角色列表
     */
    @ApiOperation(value = "查询角色列表")
    @GetMapping("exclude")
    public RspMessage<List<DeptPageResVo>> exclude(@ApiParam("部门id") @RequestParam("deptId") String deptId){
        DeptPageReqVo reqVo = new DeptPageReqVo();
        List<DeptPageResVo> list = deptService.findAll(reqVo);

        return RspMessage.success(list.stream().filter(p-> ObjectUtil.notEqual(p.getId(),deptId)).toList());
    }

    /**
     * 查询角色列表
     */
    @ApiOperation(value = "查询部门树")
    @GetMapping("findDeptTree")
    public RspMessage<List<DeptTreeVo>> findDeptTree(){

        return RspMessage.success(deptService.findTree());
    }

}
